import {
	defineStore
} from 'pinia';
import {
	myOrderList,
	orderList,
	actionOrder,
	takeOrder,
	orderDetail
} from '@/apis/order.js'

export const useOrderStore = defineStore('order', {
	state: () => {
		return {
			list: [],
			myList: [],
			detail: {
				id: 0,
				items: [],
				logs: [],
				plan_commission_amount: 0,
				receiver_location: {
					address: "",
					latitude: "",
					longitude: "",
					name: "",
					phone: "",
					shop_company_receiver_radius: 0
				},
				shop_company_location: {
					address: "",
					latitude: "",
					longitude: "",
					name: "",
					phone: false
				},
				state: ""
			},
		}
	},

	getters: {},


	actions: {
		// 获取订单列表
		async fetchOrderList(params) {
			const response = await orderList(params);
			if (response.result.success) {
				this.list = response.result.data;
			}
		},
		// 获取自己的订单列表
		async fetchMyOrderList(params) {
			const res = await myOrderList(params);
			if (res.result.success) {
				this.myList = res.result.data.records;
			}
		},
		// 接单
		async fetchTakeOrder(data) {
			const res = await takeOrder(data);
			if (res.result.success) {
				uni.$u.toast('接单成功');
			}
			return Promise.resolve(res.result.success);
		},
		// 骑手取件、送达操作
		async fetchActionOrder(data) {
			const res = await actionOrder(data);
			if (res.result.success) {
				uni.$u.toast('取件成功');
			}
			return Promise.resolve(res.result.success);
		},
		// 订单详情
		async fetchOrderDetail(params) {
			const res = await orderDetail(params);
			if (res.result.success) {
				this.detail = res.result.data;
			}
			return Promise.resolve(res.result.success);
		}
	}
});